package service

import (
	"center-platform/internal/controller/v1/post/entity"
)

type PostImpl interface {
	Info(id int) (error, *entity.Post)
	List(param *entity.SearchParam) (error, []entity.Post)
	Add(post *entity.Post) error
}

type PostService struct {
}

func (p *PostService) Info(id int) (error, *entity.Post) {
	var post entity.Post
	err, posts := post.GetInfoById(id)
	if err != nil {
		return err, nil
	}
	return nil, posts
}

func (p *PostService) List(param *entity.SearchParam) (error, []entity.Post) {
	var post entity.Post
	var where = " true"
	var args []interface{}
	limit := param.PageSize
	offset := (param.Page - 1) * limit
	if len(param.Ids) > 0 {
		where += " and id in ?"
		args = append(args, param.Ids)
	}
	if len(param.Title) > 0 {
		where += " and title like ?"
		args = append(args, "%"+param.Title+"%")
	}
	if len(param.Status) > 0 {
		where += " and status in ?"
		args = append(args, param.Status)
	}
	if len(param.UserIds) > 0 {
		where += " and user_id in ?"
		args = append(args, param.UserIds)
	}
	//if len(param.Tags) > 0 {
	//	where += " and tags in"
	//}
	err, lists := post.List(where, offset, limit, args...)
	if err != nil {
		return err, nil
	}
	return nil, lists
}
func (p *PostService) Add(post *entity.Post) error {
	return post.Create()
}
